package com.niiwoo.civet.trade.service.dubbo.newRefund;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.RefundWithholdRequest;
import com.niiwoo.civet.trade.dao.mapper.RefundWithholdRequestMapperExt;
import com.niiwoo.civet.trade.dto.request.RefundWithholdRequestDTO;
import com.niiwoo.civet.trade.service.local.newRefund.dispatch.WithholdLocalService;
import com.niiwoo.civet.trade.service.newRefund.RefundWithholdDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 代扣
 * @author wangjun
 */
@Slf4j
@Service(version = "1.0.0")
public class RefundWithholdDubboServiceImpl implements RefundWithholdDubboService
{
    @Autowired
    protected WithholdLocalService withholdLocalService;
    @Autowired
    private RefundWithholdRequestMapperExt refundWithholdRequestMapper;
    @Autowired
    protected SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public Long withhold(RefundWithholdRequestDTO requestDTO) throws Exception
    {
        log.info("RefundWithholdDubboService ->withhold request:projectId={},param={}", requestDTO.getProjectId(),requestDTO);
        RefundWithholdRequest withholdRequest = new RefundWithholdRequest();
        try
        {
            BeanUtils.copyProperties(requestDTO,withholdRequest);
            withholdRequest.setId(snowflakeIdWorker.nextId());
            withholdRequest.setOptType(requestDTO.getOptType().getValue());
            withholdRequest.setRefundType(requestDTO.getRefundType().getRefundType());
            withholdRequest.setTradeSource(requestDTO.getTradeSource().getValue());
            withholdRequest.setPeriods(StringUtils.join(requestDTO.getPeriods(),","));
            withholdRequest.setStatus(RefundWithholdRequestDTO.WithholdOrderProcessingEnum.PROCESSING.getValue());
            Date date = new Date();
            withholdRequest.setCreateTime(date);
            withholdRequest.setUpdateTime(date);
            refundWithholdRequestMapper.insertSelective(withholdRequest);

            Long orderId =  withholdLocalService.withhold(requestDTO);
            log.info("RefundWithholdDubboService ->withhold response:projectId={},orderId={}", requestDTO.getProjectId(),orderId);

            withholdRequest.setStatus(RefundWithholdRequestDTO.WithholdOrderProcessingEnum.SUCCESS.getValue());
            refundWithholdRequestMapper.updateByPrimaryKeySelective(withholdRequest);
            return orderId;
        }
        catch (Exception e)
        {
            log.error("RefundWithholdDubboService -> withhold error:projectId={}", requestDTO.getProjectId(),e);
            withholdRequest.setStatus(RefundWithholdRequestDTO.WithholdOrderProcessingEnum.FAILURE.getValue());
            // 异常消息处理
            if (e instanceof BizException) {
                BizException bizException = (BizException) e;
                withholdRequest.setErrorCode(bizException.getErrorCode());
                withholdRequest.setErrorMessage(bizException.getMessage());
                refundWithholdRequestMapper.updateByPrimaryKeySelective(withholdRequest);
                withholdLocalService.withholdFailNotify(requestDTO.getProjectId(), requestDTO.getOrgRequestNo(), requestDTO.getTradeSource().getValue(), ((BizException) e).getMessage());
            } else {
                withholdRequest.setErrorCode("-1");
                withholdRequest.setErrorMessage("系统异常");
                refundWithholdRequestMapper.updateByPrimaryKeySelective(withholdRequest);
                withholdLocalService.withholdFailNotify(requestDTO.getProjectId(), requestDTO.getOrgRequestNo(), requestDTO.getTradeSource().getValue(), "创建代扣订单失败");
            }

            throw e;
        }
    }
}
